import axios from 'axios'
import Message from '@/components/message/message.vue'
const request = axios.create({
  baseURL: 'http://43.138.15.137:4000/api/',
  timeout: 10 * 1000
})
// 响应拦截器
request.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  token && (config.headers.Authorization = token)
  return config
}, error => {
  return Promise.reject(error)
})
// 响应拦截器
request.interceptors.response.use(response => {
  let { data, meta = {} } = response.data || {};
  
  if (!response.data) {
    Message.error('服务器响应异常');
    return Promise.reject(new Error('Invalid response structure'));
  }

  const status = meta.status || response.status;
  const msg = meta.msg || '请求处理失败';

  if ([200, 201, 204].includes(status)) {
    Message.success(msg);
  } else {
    Message.error(msg);
  }
  return data;
}, error => {
  Message.error(error.response?.data?.message || '网络连接异常');
  return Promise.reject(error);
});
export default request